package com.qdxwx.web.controller.ajax;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.data.service.praise.PraiseService;
import com.qdxwx.models.praise.Praise;
import com.qdxwx.web.controller.BaseController;
import com.qdxwx.web.utils.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;


/**
 * 点赞controller
 * @author developer
 */
@Controller
@RequestMapping("/ajax")
public class AjaxPraiseController extends BaseController {

	@Autowired
	private PraiseService praiseService;

	/**
	 * 添加点赞记录
	 */
	@RequestMapping("/praise/add.json")
	@ResponseBody
	public AjaxResult addPraise(HttpServletRequest request) {
		long userId = LoginUser.getUserId(request);
		if (userId == 0) {
			return AjaxResult.error("此功能需要登录后操作，请先登录！");
		}
		//查询是否点赞 过
		Praise praise = new Praise();
		praise.setTargetId(TKConvert.toLong(request.getParameter("targetId")));
		praise.setTypeId(TKConvert.toInteger(request.getParameter("typeId")));
		praise.setUserId(userId);
		int praiseCount = praiseService.countHits(praise);
		if (praiseCount > 0) {
			return AjaxResult.error("您已点赞过，请不要重复操作！");
		}
		praiseService.addPraise(praise);
		return AjaxResult.success();
	}
}